<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
	<meta name="keywords" content="Nacos,Spring Cloud" />
	<meta name="description" content="Quick Start for Nacos Spring Cloud Projects" />
	<!-- 网页标签标题 -->
	<title>Quick Start for Nacos Spring Cloud Projects</title>
	<link rel="shortcut icon" href="https://img.alicdn.com/tfs/TB1hgJpHAPoK1RjSZKbXXX1IXXa-64-64.png"/>
	<link rel="stylesheet" href="/build/documentation.css" />
</head>
<body>
	<div id="root"><div class="documentation-page" data-reactroot=""><header class="header-container header-container-normal"><div class="header-body"><a href="/en-us/index.html"><img class="logo" src="/img/nacos_colorful.png"/></a><div class="search search-normal"><span class="icon-search"></span></div><span class="language-switch language-switch-normal">中</span><div class="header-menu"><img class="header-menu-toggle" src="/img/menu_gray.png"/><ul><li class="menu-item menu-item-normal"><a href="/en-us/index.html">HOME</a></li><li class="menu-item menu-item-normal menu-item-normal-active"><a href="/en-us/docs/quick-start.html">DOCS</a></li><li class="menu-item menu-item-normal"><a href="/en-us/blog">BLOG</a></li><li class="menu-item menu-item-normal"><a href="/en-us/community">COMMUNITY</a></li><li class="menu-item menu-item-normal"><a href="https://cn.aliyun.com/product/aliware/mse?spm=nacos-website.topbar.0.0.0">NACOS IN CLOUD</a><img class="menu-img" src="https://img.alicdn.com/tfs/TB1esl_m.T1gK0jSZFrXXcNCXXa-200-200.png"/></li><li class="menu-item menu-item-normal"><a href="http://console.nacos.io/nacos/index.html">DEMO-CONSOLE</a></li></ul></div></div></header><div class="bar"><div class="bar-body"><img src="/img/docs.png" class="front-img"/><div class="bar-title"><span>Documentation</span><div class="bone bone-light"></div></div><img src="/img/docs.png" class="back-img"/></div></div><section class="content-section"><div class="content-body"><div class="sidemenu"><div class="sidemenu-toggle"><img src="https://img.alicdn.com/tfs/TB1E6apXHGYBuNjy0FoXXciBFXa-200-200.png"/></div><ul><li class="menu-item menu-item-level-1"><span>Nacos </span><ul><li style="height:180px;overflow:hidden" class="menu-item menu-item-level-2"><span>What is Nacos<img style="transform:rotate(0deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/what-is-nacos.html" target="_self">What is Nacos</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/concepts.html" target="_self">Concepts</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/architecture.html" target="_self">Architecure</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/feature-list.html" target="_self">Feature list</a></li></ul></li><li style="height:324px;overflow:hidden" class="menu-item menu-item-level-2"><span>Quick Start<img style="transform:rotate(0deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/quick-start.html" target="_self">Nacos</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/quick-start-spring.html" target="_self">Nacos Spring</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/quick-start-spring-boot.html" target="_self">Nacos Spring Boot</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/quick-start-spring-cloud.html" target="_self">Nacos Spring Cloud</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/quick-start-docker.html" target="_self">Nacos Docker</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/use-nacos-with-dubbo.html" target="_self">Nacos with Dubbo</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/use-nacos-with-kubernetes.html" target="_self">Nacos with K8s</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-sync.html" target="_self">Nacos Sync</a></li></ul></li><li style="height:252px;overflow:hidden" class="menu-item menu-item-level-2"><span>User Guide<img style="transform:rotate(0deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/sdk.html" target="_self">Java SDK</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/other-language.html" target="_self">Other Language</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/open-api.html" target="_self">Open-API</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-spring.html" target="_self">Nacos Spring</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/system-configurations.html" target="_self">Nacos System Configurations</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/faq.html" target="_self">FAQ</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>Admin Guide<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/deployment.html" target="_self">Deployment Guide</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/cluster-mode-quick-start.html" target="_self">Cluster Mode Deployment</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/management-api.html" target="_self">Management OpenAPI</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/console-guide.html" target="_self">Admin Console Guide</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/monitor-guide.html" target="_self">Nacos Monitor Guide</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-config-benchmark.html" target="_self">Nacos Config Benchmark</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-naming-benchmark.html" target="_self">Nacos Naming Benchmark</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-sync-use.html" target="_self">Data Migration to Nacos</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>Contributor Guide<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/contributing.html" target="_self">Contribute to Nacos</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/contributing-flow.html" target="_self">Contributing Flow</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/pull-request.html" target="_self">Pull Request Template</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/how-to-reporting-bugs.html" target="_self">How to report bugs</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>Community<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/community.html" target="_self">Community</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-dev.html" target="_self">Develop Team</a></li></ul></li></ul></li></ul></div><div class="doc-content markdown-body"><h1>Quick Start for Nacos Spring Cloud Projects</h1>
<p>This quick start introduces how to enable Nacos configuration management and service discovery features for your Spring Cloud project.</p>
<p>For more details about Nacos Spring Cloud: <a href="https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-config.adoc">Nacos Config</a> and <a href="https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-discovery.adoc">Nacos Discovery</a>.</p>
<p>The quick start includes two samples:</p>
<ul>
<li>How to enable dynamic configuration updates with Nacos server and spring-cloud-starter-alibaba-nacos-config;</li>
<li>How to enable service registration and discovery with Nacos server and spring-cloud-starter-alibaba-nacos-discovery.</li>
</ul>
<h2>Prerequisite</h2>
<p>Follow instructions in <a href="https://nacos.io/zh-cn/docs/quick-start.html">Nacos Quick Start</a> to download Nacos and start the Nacos server.</p>
<h2>Enable Configuration Service</h2>
<p>Once you start the Nacos server, you can follow the steps below to enable the Nacos configuration management service for your Spring Cloud project.</p>
<p>Sample project: <a href="https://github.com/nacos-group/nacos-examples/tree/master/nacos-spring-cloud-example/nacos-spring-cloud-config-example">nacos-spring-cloud-config-example</a></p>
<ol>
<li>Add the Nacos Spring Cloud dependency.</li>
</ol>
<pre><code>&lt;dependency&gt;
    &lt;groupId&gt;com.alibaba.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-alibaba-nacos-config&lt;/artifactId&gt;
    &lt;version&gt;${latest.version}&lt;/version&gt;
&lt;/dependency&gt;
</code></pre>
<p><strong>Note</strong>: Version <a href="https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-config">2.1.x.RELEASE</a> is compatible with the Spring Boot 2.1.x line. Version <a href="https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-config">2.0.x.RELEASE</a> is compatible with the Spring Boot 2.0.x line. Version <a href="https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-config">1.5.x.RELEASE</a> is compatible with the Spring Boot 1.5.x line.</p>
<ol start="2">
<li>Configure the Nacos Server address and Specify the application name in <code>bootstrap.properties</code> :</li>
</ol>
<pre><code>spring.cloud.nacos.config.server-addr=127.0.0.1:8848

spring.application.name=example
</code></pre>
<p><strong>Note</strong>: The value of <code>spring.application.name</code> will be used to construct part of the dataId in Nacos configuration management.</p>
<p>In Nacos Spring Cloud, the format of <code>dataId</code> is as follows:</p>
<pre><code class="language-plain">${prefix}-${spring.profiles.active}.${file-extension}
</code></pre>
<ul>
<li>The value of <code>prefix</code> is the value of <code>spring.application.name</code> by default. You can also configure this value in <code>spring.cloud.nacos.config.prefix</code>.</li>
<li><code>spring.profiles.active</code> is the profile of the current environment. For more details, refer to <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles">Spring Boot Document</a>.
<strong>Note: When the value of <code>spring.profiles.active</code> is empty, the corresponding hyphen <code>-</code> will be deleted, and the format of dataId becomes: <code>${prefix}.${file-extension}</code></strong></li>
<li><code>file-exetension</code> is the data format of the configuration content, and can be configured in <code>spring.cloud.nacos.config.file-extension</code> . Currently only the <code>properties</code> and <code>yaml</code> type is supported.</li>
</ul>
<ol start="4">
<li>Add the native <code>@RefreshScope</code> annotation of Spring Cloud to enable autorefresh of configuration updates:</li>
</ol>
<pre><code>@RestController
@RequestMapping(&quot;/config&quot;)
@RefreshScope
public class ConfigController {

    @Value(&quot;${useLocalCache:false}&quot;)
    private boolean useLocalCache;

    @RequestMapping(&quot;/get&quot;)
    public boolean get() {
        return useLocalCache;
    }
}
</code></pre>
<ol start="5">
<li>Call <a href="https://nacos.io/zh-cn/docs/open-api.html">Nacos Open API</a> to publish a configuration to the Nacos server. Assume the dataId is <code>example.properties</code>，and the content is <code>useLocalCache=true</code>.</li>
</ol>
<pre><code>curl -X POST &quot;http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example.properties&amp;group=DEFAULT_GROUP&amp;content=useLocalCache=true&quot;
</code></pre>
<ol start="6">
<li>
<p>Run <code>NacosConfigApplication</code>and call  <code>curl http://localhost:8080/config/get</code>，You will get a returned value of <code>true</code>.</p>
</li>
<li>
<p>Call <a href="https://nacos.io/zh-cn/docs/open-api.html">Nacos Open API</a> again to publish an updated configuration to the Nacos server. Assume the dataId is<code>example.properties</code>，and the content is <code>useLocalCache=false</code>.</p>
</li>
</ol>
<pre><code>curl -X POST &quot;http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example.properties&amp;group=DEFAULT_GROUP&amp;content=useLocalCache=false&quot;
</code></pre>
<ol start="8">
<li>Access <code>http://localhost:8080/config/get</code>again and the returned value became <code>false</code>，indicating that the value of <code>useLocalCache</code>in your application has been updated.</li>
</ol>
<h2>Enable Service Discovery</h2>
<p>Now you would also like to enable the service discovery feature of Nacos in your Spring Cloud project.</p>
<p>Sample project: <a href="https://github.com/nacos-group/nacos-examples/tree/master/nacos-spring-cloud-example/nacos-spring-cloud-discovery-example">nacos-spring-cloud-discovery-example</a></p>
<ol>
<li>Add the Nacos Spring Cloud dependency.</li>
</ol>
<pre><code>&lt;dependency&gt;
    &lt;groupId&gt;com.alibaba.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-alibaba-nacos-discovery&lt;/artifactId&gt;
    &lt;version&gt;${latest.version}&lt;/version&gt;
&lt;/dependency&gt;
</code></pre>
<p><strong>Note</strong>: Version <a href="https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery">2.1.x.RELEASE</a> is compatible with the Spring Boot 2.1.x line. Version <a href="https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery">2.0.x.RELEASE</a> is compatible with the Spring Boot 2.0.x line. Version <a href="https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery">1.5.x.RELEASE</a> is compatible with the Spring Boot 1.5.x line.</p>
<ol start="2">
<li>Configure the service provider, so that it can register its services to the Nacos server.</li>
</ol>
<p>i. Add the Nacos server address in <code>application.properties</code> :</p>
<pre><code>server.port=8070
spring.application.name=service-provider

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
</code></pre>
<p>ii. Enable service discovery by adding the Spring Cloud native annotation of <code>@EnableDiscoveryClient</code>:</p>
<pre><code>@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {

	public static void main(String[] args) {
		SpringApplication.run(NacosProviderApplication.class, args);
	}

	@RestController
	class EchoController {
		@RequestMapping(value = &quot;/echo/{string}&quot;, method = RequestMethod.GET)
		public String echo(@PathVariable String string) {
			return &quot;Hello Nacos Discovery &quot; + string;
		}
	}
}
</code></pre>
<ol start="3">
<li>Configure the service consumer so that it can discover the services that it would like to call on the Nacos server.</li>
</ol>
<p>i. Configure the Nacos server address in <code>application.properties</code> :</p>
<pre><code>server.port=8080
spring.application.name=service-consumer

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
</code></pre>
<p>ii. Add the Spring Cloud native annotation of <code>@EnableDiscoveryClient</code>  to enable service discovery. Add the <code>@LoadBalanced</code> annotation for the <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-resttemplate.html">RestTemplate</a> instance,  and enable the integration of <code>@LoadBalanced</code> and <a href="https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-ribbon.html">Ribbon</a>:</p>
<pre><code>@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }

    @RestController
    public class TestController {

        private final RestTemplate restTemplate;

        @Autowired
        public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}

        @RequestMapping(value = &quot;/echo/{str}&quot;, method = RequestMethod.GET)
        public String echo(@PathVariable String str) {
            return restTemplate.getForObject(&quot;http://service-provider/echo/&quot; + str, String.class);
        }
    }
}
</code></pre>
<ol start="4">
<li>Start <code>ProviderApplication</code> and <code>ConsumerApplication</code> , and call <code>http://localhost:8080/echo/2018</code>. You will get a returned message of <code>Hello Nacos Discovery 2018</code>.</li>
</ol>
<h2>Related Projects</h2>
<ul>
<li><a href="https://github.com/alibaba/nacos">Nacos</a></li>
<li><a href="https://github.com/nacos-group/nacos-spring-project">Nacos Spring</a></li>
<li><a href="https://github.com/nacos-group/nacos-spring-boot-project">Nacos Spring Boot</a></li>
<li><a href="https://github.com/alibaba/spring-cloud-alibaba">Spring Cloud Alibaba</a></li>
</ul>
</div></div></section><footer class="footer-container"><div class="footer-body"><img src="/img/nacos_gray.png"/><div class="cols-container"><div class="col col-12"><h3>Vision</h3><p>By providing an easy-to-use service infrastructure such as dynamic service discovery, service configuration, service sharing and management and etc., Nacos help users better construct, deliver and manage their own service platform, reuse and composite business service faster and deliver value of business innovation more quickly so as to win market for users in the era of cloud native and in all cloud environments, such as private, mixed, or public clouds.</p></div><div class="col col-6"><dl><dt>Documentation</dt><dd><a href="/en-us/docs/what-is-nacos.html" target="_self">Overview</a></dd><dd><a href="/en-us/docs/quick-start.html" target="_self">Quick start</a></dd><dd><a href="/en-us/docs/contributing.html" target="_self">Developer guide</a></dd></dl></div><div class="col col-6"><dl><dt>Resources</dt><dd><a href="/en-us/community/index.html" target="_self">Community</a></dd><dd><a href="https://www.aliyun.com/product/acm?source_type=nacos_pc_20181219" target="_self">Cloud Service ACM</a></dd><dd><a href="https://www.aliyun.com/product/edas?source_type=nacos_pc_20181219" target="_self">Cloud Service EDAS</a></dd><dd><a href="https://www.aliyun.com/product/ahas?source_type=nacos_pc_20190225" target="_self">Cloud Service AHAS</a></dd></dl></div></div><div class="copyright"><span>@ 2018 The Nacos Authors | An Alibaba Middleware (Aliware) Project</span></div></div></footer></div></div>
	<script src="https://f.alicdn.com/react/15.4.1/react-with-addons.min.js"></script>
	<script src="https://f.alicdn.com/react/15.4.1/react-dom.min.js"></script>
	<script>
		window.rootPath = '';
  </script>
	<script src="/build/documentation.js"></script>
</body>
</html>